package com.cn.kabalifemerchants.ui.user;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.LazyFragment;
import com.cn.kabalifemerchants.config.CustomRefreshListener;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.config.RefreshHelper;
import com.cn.kabalifemerchants.ui.user.activity.IMoneyActivity;
import com.cn.kabalifemerchants.ui.user.activity.NwsMsgActivity;
import com.cn.kabalifemerchants.ui.user.activity.SystemSetActivity;
import com.cn.kabalifemerchants.ui.user.activity.UserInfoActivity;
import com.cn.kabalifemerchants.ui.user.bean.MyUserInfoBean;
import com.cn.kabalifemerchants.utils.GlideUtil;
import com.cn.kabalifemerchants.utils.RxToast;
import com.flyco.roundview.RoundTextView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 晓林 on 2020/10/14.
 * function:
 */
public class FragmentUser extends LazyFragment<FragmentUserView, FragmentUserPresenter> implements FragmentUserView {
    @BindView(R.id.tv_msg)
    RoundTextView tvMsg;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_stock)
    TextView tvUserStock;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_day_money)
    TextView tvDayMoney;
    @BindView(R.id.tv_cash_money)
    TextView tvCashMoney;
    @BindView(R.id.img_user_head)
    CircleImageView imgUserHead;
    @BindView(R.id.tv_order_count)
    TextView tvOrderCount;
    @BindView(R.id.tv_order_ok_count)
    TextView tvOrderOkCount;
    @BindView(R.id.tv_fraction)
    TextView tvFraction;
    @BindView(R.id.tv_case_count)
    TextView tvCaseCount;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Override
    protected FragmentUserPresenter createPresenter() {
        return new FragmentUserPresenter();
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_home_user;
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onUserInfoSuccess(MyUserInfoBean bean) {
        if (refreshLayout != null)
            refreshLayout.finishRefresh();
        tvUserName.setText(bean.getResult().getNickname());
        GlideUtil.ImageLoad(getMe(), bean.getResult().getHead(), imgUserHead);
        tvMoney.setText(String.valueOf(bean.getResult().getMoney()));
        tvDayMoney.setText(String.valueOf(bean.getResult().getDay_money()));
        tvCashMoney.setText(String.valueOf(bean.getResult().getCash_money()));
        tvOrderCount.setText(String.valueOf(bean.getResult().getOrder_count()));
        tvOrderOkCount.setText(String.valueOf(bean.getResult().getOrder_ok_count()));
        tvFraction.setText(String.valueOf(bean.getResult().getFraction()));
        tvCaseCount.setText(String.valueOf(bean.getResult().getCase_count()));
        if (bean.getResult().getNotify_count() > 0) {
            tvMsg.setVisibility(View.VISIBLE);
            tvMsg.setText(String.valueOf(bean.getResult().getNotify_count()));
        } else tvMsg.setVisibility(View.GONE);

    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ButterKnife.bind(this, view);
        initRefreshLayout();
    }

    @OnClick({R.id.ll_btn_set, R.id.ll_btn_nws, R.id.ll_btn_sys_set, R.id.ll_btn_money})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_btn_money:
                $startActivity(IMoneyActivity.class);
                break;
            case R.id.ll_btn_sys_set:
                //通用设置
                $startActivity(SystemSetActivity.class);
                break;
            case R.id.ll_btn_set:
                //todo 设置
                $startActivity(UserInfoActivity.class);
                break;
            case R.id.ll_btn_nws:
                //todo 消息
                $startActivity(NwsMsgActivity.class);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.onUserInfoData(LoginHelper.getLonginData().getResult());
    }

    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                mPresenter.onUserInfoData(LoginHelper.getLonginData().getResult());
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {

            }
        });
    }
}
